package com.ruoyi.material.service.impl;

import java.util.List;

import com.ruoyi.common.constant.material.PurchaseConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.material.domain.PurchaseOrder;
import com.ruoyi.material.domain.PurchaseOrderInspect;
import com.ruoyi.material.mapper.PurchaseOrderInspectMapper;
import com.ruoyi.material.mapper.PurchaseOrderMapper;
import com.ruoyi.material.service.IPurchaseOrderInspectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 采购单查验记录Service业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class PurchaseOrderInspectServiceImpl implements IPurchaseOrderInspectService {
    @Resource
    private PurchaseOrderInspectMapper purchaseOrderInspectMapper;
    @Resource
    private PurchaseOrderMapper purchaseOrderMapper;

    /**
     * 查询采购单查验记录
     *
     * @param id 采购单查验记录主键
     * @return 采购单查验记录
     */
    @Override
    public PurchaseOrderInspect selectPurchaseOrderInspectById(Long id) {
        return purchaseOrderInspectMapper.selectPurchaseOrderInspectById(id);
    }

    /**
     * 查询采购单查验记录列表
     *
     * @param purchaseOrderInspect 采购单查验记录
     * @return 采购单查验记录
     */
    @Override
    public List<PurchaseOrderInspect> selectPurchaseOrderInspectList(PurchaseOrderInspect purchaseOrderInspect) {
        return purchaseOrderInspectMapper.selectPurchaseOrderInspectList(purchaseOrderInspect);
    }

    /**
     * 新增采购单查验记录
     *
     * @param purchaseOrderInspect 采购单查验记录
     * @return 结果
     */
    @Override
    public int insertPurchaseOrderInspect(PurchaseOrderInspect purchaseOrderInspect) {
        purchaseOrderInspect.setCreateTime(DateUtils.getNowDate());
        int i = purchaseOrderInspectMapper.insertPurchaseOrderInspect(purchaseOrderInspect);
        if (i > 0) {
            if (purchaseOrderInspect.getParams().containsKey("status")) {
                PurchaseOrder purchaseOrder = new PurchaseOrder();
                purchaseOrder.setId(purchaseOrderInspect.getOrderId());
                //通过
                if (Integer.parseInt(purchaseOrderInspect.getParams().get("status").toString()) == 0) {
                    purchaseOrder.setStatus(PurchaseConstants.StatusEnum.Accepted);
                }
                //拒绝
                else {
                    purchaseOrder.setStatus(PurchaseConstants.StatusEnum.Refused);
                }
                purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
            }
        }
        return i;
    }

    /**
     * 修改采购单查验记录
     *
     * @param purchaseOrderInspect 采购单查验记录
     * @return 结果
     */
    @Override
    public int updatePurchaseOrderInspect(PurchaseOrderInspect purchaseOrderInspect) {
        purchaseOrderInspect.setUpdateTime(DateUtils.getNowDate());
        return purchaseOrderInspectMapper.updatePurchaseOrderInspect(purchaseOrderInspect);
    }

    /**
     * 批量删除采购单查验记录
     *
     * @param ids 需要删除的采购单查验记录主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderInspectByIds(Long[] ids) {
        return purchaseOrderInspectMapper.deletePurchaseOrderInspectByIds(ids);
    }

    /**
     * 删除采购单查验记录信息
     *
     * @param id 采购单查验记录主键
     * @return 结果
     */
    @Override
    public int deletePurchaseOrderInspectById(Long id) {
        return purchaseOrderInspectMapper.deletePurchaseOrderInspectById(id);
    }
}
